package com.elite.groovy.dev;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.ArrayList;
import java.util.Collection;

@Configuration  //声明这是一个配置类
@ComponentScan(basePackages = {"senvice","dao"})  //开启src下的senvice包和dao的注解扫描
@EnableTransactionManagement  //开启事务
public class TxConfigAvaya {
    //创建数据库连接池
    @Bean
    public static DruidDataSource getDruidDataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");  //设置驱动 直接照抄就好
        druidDataSource.setUrl("jdbc:oracle:thin:@10.248.100.20:1521:wxuat");   //数据库路径
        druidDataSource.setUsername("elite");    //数据库用户名
        druidDataSource.setPassword("ELITE##2024");    //数据库密码
        Collection<Object> connectionInitSqls = new ArrayList<>();
        connectionInitSqls.add("alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS'");

        druidDataSource.setConnectionInitSqls(connectionInitSqls);    //数据库密码
        return druidDataSource;
    }
    //创建jdbcTemplate对象
    @Bean
    public static JdbcTemplate getJdbcTemplate() {
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(getDruidDataSource());
        return jdbcTemplate;
    }
    //创建事务管理器对象
    @Bean
    public DataSourceTransactionManager getDataSourceTransactionManager() {
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(getDruidDataSource());
        return dataSourceTransactionManager;
    }
}
